<?php

namespace Blog\Controller;

use Hx\Mvc\Controller\Base;

/**
 * Class Api
 * @property \Blog\Model\Category categoryModel
 * @property \Blog\Model\User userModel
 * @property \Blog\Model\Blog blogModel
 *
 * @package Blog\Controller
 */
class Api extends Base {

    private function setJson($data) {
        $this->response->setContentType('application/json;charset=utf-8')
            ->setBody(json_encode($data));
        return $this->response;
    }

    private function setResponseFail($message) {
        return $this->setJson(array('response' => FALSE, 'message' => $message));
    }

    private function setResponseSuccess($data, $keyName = 'data') {
        return $this->setJson(array('response' => TRUE, $keyName => $data));
    }

    public function allAction() {
        $api = array(
            'user-authorize'  => array('url' => 'authorize.json', 'method' => 'POST'),
            'user-check'      => array('url' => 'authorize.json', 'method' => 'PUT'),
            'blog-create'     => array('url' => 'blog.json', 'method' => 'PUT'),
            'blog-edit'       => array('url' => 'blog.json', 'method' => 'POST'),
            'blog-get'        => array('url' => 'blog.json', 'method' => 'GET'),
            'blog-delete'     => array('url' => 'blog.json', 'method' => 'DELETE'),
            'category-create' => array('url' => 'category.json', 'method' => 'PUT'),
            'category-edit'   => array('url' => 'category.json', 'method' => 'POST'),
            'category-get'    => array('url' => 'category.json', 'method' => 'GET'),
            'category-delete' => array('url' => 'category.json', 'method' => 'DELETE')
        );
        return $this->setJson($api);
    }

    public function postAction($opt) {
        if ($opt === 'authorize') {
            return $this->_doUserAuthorize();
        }
        if ($opt === 'blog') {
            return $this->_doBlogSave();
        }
        return $this->setError(404);
    }

    public function putAction($opt) {
        if ($opt === 'authorize') {
            return $this->_doUserCheck();
        }
        if ($opt === 'blog') {
            return $this->_doBlogSave();
        }
        return $this->setError(404);
    }

    public function deleteAction($opt) {
        var_dump($opt);
    }

    public function getAction($opt) {
        if ($opt === 'category') {
            return $this->_doCategoryGet($this->input->get(TRUE));
        }
        if ($opt === 'blog') {
            return $this->_doBlogGet($this->input->get(TRUE));
        }
        return $this->setError(404);
    }

    //---------------

    private function _doUserAuthorize() {
        $user = $this->input->post('user');
        $password = $this->input->post('password');
        $expire = $this->input->post('expire:int');
        $app = $this->request->getHeader('X-APP');
        $res = $this->userModel->authorize($user, $password, $expire, $app);
        if (is_int($res)) {
            return $this->setResponseFail('用户或密码错误');
        }
        return $this->setResponseSuccess($res, 'user');
    }

    private function _doUserCheck() {
        $user = $this->request->getHeader('X-User');
        $token = $this->request->getHeader('X-Token');
        $app = $this->request->getHeader('X-APP');
        if ($this->userModel->check($user, $token, $app)) {
            return $this->setResponseSuccess(TRUE, 'token');
        }
        return $this->setResponseFail('密匙无效');
    }

    //-------------

    private function _doBlogSave() {
        $data = $this->input->post(TRUE);
        if ($data['change']) {
            return $this->setResponseSuccess($this->blogModel->changeStatus($data['id'], $data['status']), 'change');
        }
        $data['userId'] = $this->request->getHeader('X-User');
        $res = $this->blogModel->saveOne($data);
        if ($res === FALSE) {
            return $this->setResponseFail('参数错误');
        }
        return $this->setResponseSuccess($res, 'blog');
    }

    private function _doBlogGet($param) {
        if ($param['id']) {
            return $this->setResponseSuccess($this->blogModel->getById($param['id']), 'blog');
        }
        return $this->setError(404);
    }

    //-------------------

    private function _doCategoryGet($param) {
        if ($param['all'] == TRUE) {
            return $this->setResponseSuccess($this->categoryModel->getRank(), 'category');
        }
        if ($param['id']) {
            return $this->setResponseSuccess($this->categoryModel->getById((int)$param['id']), 'category');
        }
        if ($param['slug']) {
            return $this->setResponseSuccess($this->categoryModel->getBySlug($param['slug']), 'category');
        }
        return $this->setError(404);
    }

}